import { query } from '../../config/db.js';
import logger from '../../config/logger.js';

// 常见中文姓氏和名字
const lastName = ['王', '李', '张', '刘', '陈', '杨', '赵', '黄', '周', '吴', '徐', '孙', '胡', '朱', '高', '林', '何', '郭', '马', '罗'];
const firstNameMale = ['伟', '强', '军', '勇', '杰', '涛', '磊', '超', '明', '刚', '辉', '强', '军', '杰', '浩', '轩', '博', '宇', '俊', '涵'];
const firstNameFemale = ['芳', '娜', '婷', '静', '敏', '莉', '兰', '玲', '燕', '娟', '艳', '瑞', '瑶', '洁', '梅', '玲', '雪', '娜', '丽', '丹'];

// 班级信息
const classes = [
  { name: '395', headTeacher: '张明', headTeacherPhone: '13800138001', location: '教学楼A栋301室' },
  { name: '396', headTeacher: '李华', headTeacherPhone: '13900139002', location: '教学楼A栋302室' },
  { name: '397', headTeacher: '王芳', headTeacherPhone: '13700137003', location: '教学楼A栋303室' },
  { name: '398', headTeacher: '赵刚', headTeacherPhone: '13600136004', location: '教学楼A栋401室' },  
  { name: '399', headTeacher: '陈明', headTeacherPhone: '13500135005', location: '教学楼A栋402室' },
];

// 生成随机姓名
function generateName(gender) {
  const lastNameIndex = Math.floor(Math.random() * lastName.length);
  const firstNameArray = gender === '男' ? firstNameMale : firstNameFemale;
  const firstNameIndex = Math.floor(Math.random() * firstNameArray.length);
  return lastName[lastNameIndex] + firstNameArray[firstNameIndex];
}

// 生成随机备注
function generateRemark() {
  const remarks = ['优秀学生', '积极参与活动', '成绩良好', '需要加强数学', '体育特长生', '艺术爱好者', '新转来的学生', ''];
  return remarks[Math.floor(Math.random() * remarks.length)];
}

async function seedNewStudents() {
  try {
    logger.info('开始播种新生数据...');

    // 先清空新生表
    // 禁用外键约束
    await query('SET FOREIGN_KEY_CHECKS = 0');
    await query('TRUNCATE TABLE new_student_classes');
    // 启用外键约束
    await query('SET FOREIGN_KEY_CHECKS = 1');
    // 重置自增序列
    await query('ALTER TABLE new_student_classes AUTO_INCREMENT = 1');
    logger.info('新生表已清空');

    // 生成22个新生数据
    const newStudents = [];
    for (let i = 0; i < 22; i++) {
      const gender = Math.random() > 0.5 ? '男' : '女';
      const classNameInfo = classes[Math.floor(Math.random() * classes.length)];
      newStudents.push({
        student_name: generateName(gender),
        gender: gender,
        class_name: classNameInfo.name,
        head_teacher: classNameInfo.headTeacher,
        head_teacher_phone: classNameInfo.headTeacherPhone,
        class_location: classNameInfo.location,
        remarks: generateRemark()
      });
    }

    // 插入新生数据
    for (const student of newStudents) {
      await query(
        'INSERT INTO new_student_classes (student_name, gender, class_name, head_teacher, head_teacher_phone, class_location, remarks) VALUES (?, ?, ?, ?, ?, ?, ?)',
        [
          student.student_name,
          student.gender,
          student.class_name,
          student.head_teacher,
          student.head_teacher_phone,
          student.class_location,
          student.remarks
        ]
      );
    }
    logger.info('已成功插入22个新生数据');

    logger.info('新生数据播种完成');
  } catch (error) {
    logger.error('新生数据播种失败:', error);
    throw error;
  }
}

export default seedNewStudents;